package com.example.verticles;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.WorkerExecutor;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

public class BlockingVerticle extends AbstractVerticle {

    @Override
    public void start() throws Exception {
//        block01();
        WorkerExecutor executor = vertx.createSharedWorkerExecutor("executor",1,10 ,TimeUnit.SECONDS);
        executor.executeBlocking(future->{
                    long start = System.currentTimeMillis();
                    int i=1;
                    while (true){
                        if ((System.currentTimeMillis()-start)/1000==i){
                            System.out.println(LocalDateTime.now() +"========="+i);
                            i++;
                        }
                        if ((System.currentTimeMillis()-start)/1000>9){
                            break;
                        }
                    }
                    System.out.println("block end");
                    future.complete("=====blocking result=======");
                },
                res->{
                    System.out.println(LocalDateTime.now().toString()+res.result());
                });
        executor.close();
    }

    private void block01() {
        vertx.executeBlocking(id->{
            long start = System.currentTimeMillis();
            int i=1;
            while (true){
                if ((System.currentTimeMillis()-start)/1000==i){
                    System.out.println(LocalDateTime.now() +"========="+i);
                    i++;
                }
                if ((System.currentTimeMillis()-start)/1000>100){
                    break;
                }
            }
            System.out.println("block end");
        });
    }
}
